package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IBDMProductionCostDao;
import com.qymerge.manage.base.dao.IBDMVoucherDao;
import com.qymerge.manage.base.ro.BDMProductionCostRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *<p>Title: BDMProductionCostDaoImpl</p>
 *<p>Description: 生成成本</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2025/06/16
 *@Time 17:54
 */
@Repository
public class BDMProductionCostDaoImpl extends BaseDao implements IBDMProductionCostDao {

    private String dwd_MCRS_BDM_Production_Cost = "SELECT Id,CompanyId,CompanyName,PeriodYear,CostCenterCode,CostCenterName,AccountViewCode,AccountViewName,MaterialCode,MaterialName,StdPackQuantity,GroupName,ProductionOrderNumber,WorkOrderCategory,ProductionDate,WorkHours,ProductionLineNumber,TotalWorkHours,ProductionQuantity,DirectMaterial,Packaging,ExternalProcessingFee,LabourCost,Wastage,EnergyCost,DepreciationAmortization,AdministrativeExpenses,RepairCharge,SafeProductionReserve,ExtensiveRepair,TotalCost,UnitCost from dwd_MCRS_BDM_Production_Cost where 1=1 ";

    @Override
    public List list(BDMProductionCostRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_BDM_Production_Cost;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getCompanyName())) {
            sql += " and CompanyName like :CompanyName";
            paramMap.put("CompanyName", "%" + ro.getCompanyName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCostCenterCode())) {
            sql += " and CostCenterCode like :CostCenterCode";
            paramMap.put("CostCenterCode", "%" + ro.getCostCenterCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCostCenterName())) {
            sql += " and CostCenterName like :CostCenterName";
            paramMap.put("CostCenterName", "%" + ro.getCostCenterName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getAccountViewCode())) {
            sql += " and AccountViewCode like :AccountViewCode";
            paramMap.put("AccountViewCode", "%" + ro.getAccountViewCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getAccountViewName())) {
            sql += " and AccountViewName like :AccountViewName";
            paramMap.put("AccountViewName", "%" + ro.getAccountViewName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getMaterialCode())) {
            sql += " and MaterialCode like :MaterialCode";
            paramMap.put("MaterialCode", "%" + ro.getMaterialCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getMaterialName())) {
            sql += " and MaterialName like :MaterialName";
            paramMap.put("MaterialName", "%" + ro.getMaterialName() + "%");
        }

        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<BDMProductionCostRO>(BDMProductionCostRO.class));
    }

    @Override
    public int total(BDMProductionCostRO ro) {
        String sql = "select count(1) from dwd_MCRS_BDM_Production_Cost where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();


        if (StringUtils.isNotBlank(ro.getCompanyName())) {
            sql += " and CompanyName like :CompanyName";
            paramMap.put("CompanyName", "%" + ro.getCompanyName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCostCenterCode())) {
            sql += " and CostCenterCode like :CostCenterCode";
            paramMap.put("CostCenterCode", "%" + ro.getCostCenterCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCostCenterName())) {
            sql += " and CostCenterName like :CostCenterName";
            paramMap.put("CostCenterName", "%" + ro.getCostCenterName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getAccountViewCode())) {
            sql += " and AccountViewCode like :AccountViewCode";
            paramMap.put("AccountViewCode", "%" + ro.getAccountViewCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getAccountViewName())) {
            sql += " and AccountViewName like :AccountViewName";
            paramMap.put("AccountViewName", "%" + ro.getAccountViewName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getMaterialCode())) {
            sql += " and MaterialCode like :MaterialCode";
            paramMap.put("MaterialCode", "%" + ro.getMaterialCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getMaterialName())) {
            sql += " and MaterialName like :MaterialName";
            paramMap.put("MaterialName", "%" + ro.getMaterialName() + "%");
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public BDMProductionCostRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_BDM_Production_Cost);
        sql.append(" and Id=?");
        RowMapper<BDMProductionCostRO> rowMapper = new BeanPropertyRowMapper<BDMProductionCostRO>(BDMProductionCostRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public int install(BDMProductionCostRO ro) {
        StringBuilder sql = new StringBuilder("insert into dwd_MCRS_BDM_Production_Cost(CompanyId, CompanyName,PeriodYear,CostCenterCode,CostCenterName,AccountViewCode,AccountViewName,MaterialCode,MaterialName,StdPackQuantity,GroupName,ProductionOrderNumber,WorkOrderCategory,ProductionDate,WorkHours,ProductionLineNumber,TotalWorkHours,ProductionQuantity,DirectMaterial,Packaging,ExternalProcessingFee,LabourCost,Wastage,EnergyCost,DepreciationAmortization,AdministrativeExpenses,RepairCharge,SafeProductionReserve,ExtensiveRepair,TotalCost,UnitCost) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

        List params = new ArrayList();
        params.add(ro.getCompanyId());
        params.add(ro.getCompanyName());
        params.add(ro.getPeriodYear());
        params.add(ro.getCostCenterCode());
        params.add(ro.getCostCenterName());
        params.add(ro.getAccountViewCode());
        params.add(ro.getAccountViewName());
        params.add(ro.getMaterialCode());
        params.add(ro.getMaterialName());
        params.add(ro.getStdPackQuantity());
        params.add(ro.getGroupName());
        params.add(ro.getProductionOrderNumber());
        params.add(ro.getWorkOrderCategory());
        params.add(ro.getProductionDate());
        params.add(ro.getWorkHours());
        params.add(ro.getProductionLineNumber());
        params.add(ro.getTotalWorkHours());
        params.add(ro.getProductionQuantity());
        params.add(ro.getDirectMaterial());
        params.add(ro.getPackaging());
        params.add(ro.getExternalProcessingFee());
        params.add(ro.getLabourCost());
        params.add(ro.getWastage());
        params.add(ro.getEnergyCost());
        params.add(ro.getDepreciationAmortization());
        params.add(ro.getAdministrativeExpenses());
        params.add(ro.getRepairCharge());
        params.add(ro.getSafeProductionReserve());
        params.add(ro.getExtensiveRepair());
        params.add(ro.getTotalCost());
        params.add(ro.getUnitCost());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int update(BDMProductionCostRO ro) {
        StringBuilder sql = new StringBuilder("update dwd_MCRS_BDM_Production_Cost set CompanyId=:CompanyId,CompanyName=:CompanyName,PeriodYear=:PeriodYear,CostCenterCode=:CostCenterCode,CostCenterName=:CostCenterName,AccountViewCode=:AccountViewCode,AccountViewName=:AccountViewName,MaterialCode=:MaterialCode,MaterialName=:MaterialName,StdPackQuantity=:StdPackQuantity,GroupName=:GroupName,ProductionOrderNumber=:ProductionOrderNumber,WorkOrderCategory=:WorkOrderCategory,ProductionDate=:ProductionDate,WorkHours=:WorkHours,ProductionLineNumber=:ProductionLineNumber,TotalWorkHours=:TotalWorkHours,ProductionQuantity=:ProductionQuantity,DirectMaterial=:DirectMaterial,Packaging=:Packaging,ExternalProcessingFee=:ExternalProcessingFee,LabourCost=:LabourCost,Wastage=:Wastage,EnergyCost=:EnergyCost,DepreciationAmortization=:DepreciationAmortization,AdministrativeExpenses=:AdministrativeExpenses,RepairCharge=:RepairCharge,SafeProductionReserve=:SafeProductionReserve,ExtensiveRepair=:ExtensiveRepair,TotalCost=:TotalCost,UnitCost=:UnitCost ");

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("CompanyId", ro.getCompanyId());
        paramsMap.put("CompanyName", ro.getCompanyName());
        paramsMap.put("PeriodYear", ro.getPeriodYear());
        paramsMap.put("CostCenterCode", ro.getCostCenterCode());
        paramsMap.put("CostCenterName", ro.getCostCenterName());
        paramsMap.put("AccountViewCode", ro.getAccountViewCode());
        paramsMap.put("AccountViewName", ro.getAccountViewName());
        paramsMap.put("MaterialCode", ro.getMaterialCode());
        paramsMap.put("MaterialName", ro.getMaterialName());
        paramsMap.put("StdPackQuantity", ro.getStdPackQuantity());
        paramsMap.put("GroupName", ro.getGroupName());
        paramsMap.put("ProductionOrderNumber", ro.getProductionOrderNumber());
        paramsMap.put("WorkOrderCategory", ro.getWorkOrderCategory());
        paramsMap.put("ProductionDate", ro.getProductionDate());
        paramsMap.put("WorkHours", ro.getWorkHours());
        paramsMap.put("ProductionLineNumber", ro.getProductionLineNumber());
        paramsMap.put("TotalWorkHours", ro.getTotalWorkHours());
        paramsMap.put("ProductionQuantity", ro.getProductionQuantity());
        paramsMap.put("DirectMaterial", ro.getDirectMaterial());
        paramsMap.put("Packaging", ro.getPackaging());
        paramsMap.put("ExternalProcessingFee", ro.getExternalProcessingFee());
        paramsMap.put("LabourCost", ro.getLabourCost());
        paramsMap.put("Wastage", ro.getWastage());
        paramsMap.put("EnergyCost", ro.getEnergyCost());
        paramsMap.put("DepreciationAmortization", ro.getDepreciationAmortization());
        paramsMap.put("AdministrativeExpenses", ro.getAdministrativeExpenses());
        paramsMap.put("RepairCharge", ro.getRepairCharge());
        paramsMap.put("SafeProductionReserve", ro.getSafeProductionReserve());
        paramsMap.put("ExtensiveRepair", ro.getExtensiveRepair());
        paramsMap.put("TotalCost", ro.getTotalCost());
        paramsMap.put("UnitCost", ro.getUnitCost());

        sql.append("where Id=:Id");
        paramsMap.put("Id", ro.getId());
        return getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
    }

    @Override
    public BDMProductionCostRO findOneByCompanyNameAndPeriodYearAndCostCenterCodeAndMaterialCode(String companyName, String periodYear, String costCenterCode, String materialCode) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_BDM_Production_Cost);
        sql.append(" and CompanyName=?");
        sql.append(" and PeriodYear=?");
        sql.append(" and CostCenterCode=?");
        sql.append(" and MaterialCode=?");
        RowMapper<BDMProductionCostRO> rowMapper = new BeanPropertyRowMapper<BDMProductionCostRO>(BDMProductionCostRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, companyName, periodYear, costCenterCode, materialCode);
        } catch (DataAccessException e) {
            return null;
        }
    }


}
